package com.web;

import com.bean.Book;
import com.dao.BookDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/book/update")
public class bookUpdateServlet extends HttpServlet {
    /**
     * doGet 方法查询数据
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取参数
        String id = req.getParameter("id");

        // 判断 id 是否为空
        if (id == null) {
            // 把信息存到 message 中处理
            req.setAttribute("message", "参数异常！");
            // 页面转发
            req.getRequestDispatcher("/book/bookResult.jsp").forward(req, resp);
            // 返回
            return;
        }
        try {
            // 创建对象并实现方法
            Book book = new BookDao().get(Integer.parseInt(id));

            // 把信息存到 book 中处理
            req.setAttribute("book", book);
            // 页面转发
            req.getRequestDispatcher("/book/bookUpdate.jsp").forward(req, resp);
        } catch (Exception e) {
            // 把异常信息打印到控制台
            e.printStackTrace();
            // 把信息存到 message 中处理
            req.setAttribute("message", "错误信息：" + e.getLocalizedMessage());
            // 页面转发
            req.getRequestDispatcher("/book/bookResult.jsp").forward(req, resp);
        }
    }

    /***
     * doPost 请求处理修改方法
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取参数
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String author = req.getParameter("author");
        String price = req.getParameter("price");
        String press = req.getParameter("press");
        String sales = req.getParameter("sales");

        // 判断参数是否为空
        if (id == null || name == null || author == null || price == null || press == null || sales == null) {
            // 把信息存到 message 中处理
            req.setAttribute("message", "参数异常！");
            // 页面转发
            req.getRequestDispatcher("/book/bookResult.jsp").forward(req, resp);
            // 返回
            return;
        }

        try {
            // 创建对象
            BookDao bookDao = new BookDao();
            // 实现方法
            bookDao.update(Integer.parseInt(id), name, author, Float.parseFloat(price), press, sales);

            // 把信息存到 message 中处理
            req.setAttribute("message", "更新成功");
            // 页面转发
            req.getRequestDispatcher("/book/bookResult.jsp").forward(req, resp);
        } catch (Exception e) {
            // 把异常信息打印到控制台
            e.printStackTrace();
            // 把信息存到 message 中处理
            req.setAttribute("message", "错误信息：" + e.getLocalizedMessage());
            // 页面转发
            req.getRequestDispatcher("/book/bookResult.jsp").forward(req, resp);
        }
    }
}
